package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.CouponCodeDTO;
import com.quanquan.management.dto.CouponDTO;
import com.quanquan.management.entity.SysUser;
import com.quanquan.management.request.CouponPushRequestBody;
import com.quanquan.management.request.CouponRecordRequestBody;
import com.quanquan.management.request.CouponRequestBody;
import entity.ExcelData;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import utils.ExcelUtil;
import utils.Request;
import utils.StringUtil;
import web.response.ResultBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author gongzhou
 * @title: CouponService
 * @projectName SpringCloud
 * @description: TODO
 * @date 2019/4/116:16
 */
@Service
public class CouponService {
    @Value("${module-url.storeUrl}")
    private String storeUrl;

    public PageDTO<CouponDTO> list(CouponRequestBody requestBody) {
        String json = Request.postJson(storeUrl + "coupon/query_select", requestBody);
        PageDTO<CouponDTO> pageDTO = new PageDTO<>();
        if (StringUtil.isEmpty(json)) {
            return pageDTO;
        }
        JSONObject jsonObject = JSON.parseObject(json);
        if (null == jsonObject) {
            return pageDTO;
        }
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            return data;
        }
        return pageDTO;
    }

    public PageDTO<CouponCodeDTO> list(CouponRecordRequestBody requestBody) {
        String json = Request.postJson(storeUrl + "coupon/query/record_select", requestBody);
        PageDTO<CouponCodeDTO> pageDTO = new PageDTO<>();
        if (StringUtil.isEmpty(json)) {
            return pageDTO;
        }
        JSONObject jsonObject = JSON.parseObject(json);
        if (null == jsonObject) {
            return pageDTO;
        }
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<CouponCodeDTO> userDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++) {
                CouponCodeDTO object = list.getObject(i, CouponCodeDTO.class);
                userDTOS.add(object);
            }
            data.setList(userDTOS);
            return data;
        }
        return pageDTO;
    }

    public void export(HttpServletResponse response, CouponRecordRequestBody requestBody) throws Exception {
        List<String> titles = new LinkedList<>();
        titles.add("券码");
        titles.add("优惠券名称");
        titles.add("用户名称");
        titles.add("会员卡号");
        titles.add("手机号");
        titles.add("领取时间");
        titles.add("创建时间");
        titles.add("使用时间");
        titles.add("有效期");
        titles.add("核销状态");
        titles.add("核销人");
        ExcelData data = ExcelUtil.getExcelData();
        data.setTitles(titles);
        PageDTO<CouponCodeDTO> pageDTO = this.list(requestBody);
        List<CouponCodeDTO> list = pageDTO.getList();
        List<List<Object>> rows = new LinkedList<>();
        list.forEach(codeDTO -> {
            List<Object> row = new LinkedList<>();
            row.add(codeDTO.getCouponCode());
            row.add(codeDTO.getCouponName());
            row.add(codeDTO.getUseName());
            row.add(codeDTO.getMemberCardNum());
            row.add(codeDTO.getPhone());
            row.add(codeDTO.getAddTime());
            row.add(codeDTO.getCreateTime());
            row.add(codeDTO.getUseTime());
            row.add(codeDTO.getUseTimeEnd());
            if(codeDTO.getStatus()==1){
                row.add("已核销");
            }else{
                row.add("未核销");
            }
            row.add(codeDTO.getUseAdminName());
            rows.add(row);
        });
        data.setRows(rows);
        String now = DateUtils.dateToString(new Date(), DateUtils.DATE_HOUR_SECOND);
        ExcelUtil.exportExcel(response, "核销记录-" + now, data);
    }

    public boolean addCoupon(CouponRequestBody requestBody) {

        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(storeUrl + "coupon/add_coupon", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

    public ResultBody push(CouponPushRequestBody requestBody) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        requestBody.setSenderName(sysUser.getUsername());
        String json = Request.postJson(storeUrl + "coupon/send_coupon",requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        String msg = jsonObject.getString("resMsg");
        ResultBody resultBody = new ResultBody(resCode,msg);
        return resultBody;
    }

    public CouponDTO findById(String id) {
        CouponRequestBody requestBody = new CouponRequestBody();
        requestBody.setId(id);
        String json = Request.postJson(storeUrl + "coupon/query_by_id_manage", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        CouponDTO couponDTO = null;
        if (0 == resCode) {
            JSONObject data = jsonObject.getJSONObject("data");
            couponDTO = data.toJavaObject(CouponDTO.class);
            System.out.println(couponDTO);
        }
        return couponDTO;
    }

    public boolean update(CouponRequestBody requestBody) {
        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(storeUrl + "coupon/update_coupon", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

    public boolean updateStatus(CouponRequestBody requestBody) {
        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(storeUrl + "coupon/change_coupon_status",requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 != resCode) {
            return false;
        }
        return true;
    }

    public PageDTO<CouponCodeDTO> sendRecord(CouponRecordRequestBody requestBody) {
        String json = Request.postJson(storeUrl + "coupon/send_record", requestBody);
        PageDTO<CouponCodeDTO> pageDTO = new PageDTO<>();
        if (StringUtil.isEmpty(json)) {
            return pageDTO;
        }
        JSONObject jsonObject = JSON.parseObject(json);
        if (null == jsonObject) {
            return pageDTO;
        }
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<CouponCodeDTO> userDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++) {
                CouponCodeDTO object = list.getObject(i, CouponCodeDTO.class);
                userDTOS.add(object);
            }
            data.setList(userDTOS);
            return data;
        }
        return pageDTO;
    }

    public void exportSendRecord(HttpServletResponse response, CouponRecordRequestBody requestBody) throws Exception {
        List<String> titles = new LinkedList<>();
        titles.add("券码");
        titles.add("优惠券名称");
        titles.add("现金价格");
        titles.add("用户名称");
        titles.add("会员卡号");
        titles.add("手机号");
        titles.add("领取时间");
        titles.add("有效期");
        titles.add("核销状态");
        titles.add("发放人");
        ExcelData data = ExcelUtil.getExcelData();
        data.setTitles(titles);
        PageDTO<CouponCodeDTO> pageDTO = this.sendRecord(requestBody);
        List<CouponCodeDTO> list = pageDTO.getList();
        List<List<Object>> rows = new LinkedList<>();
        list.forEach(codeDTO -> {
            List<Object> row = new LinkedList<>();
            row.add(codeDTO.getCouponCode());
            row.add(codeDTO.getCouponName());
            row.add(codeDTO.getPrice());
            row.add(codeDTO.getUseName());
            row.add(codeDTO.getUseMemberCard());
            row.add(codeDTO.getPhone());
            row.add(codeDTO.getAddTime());
            row.add(codeDTO.getUseTimeEnd());
            if(codeDTO.getStatus()==1){
                row.add("已核销");
            }else{
                row.add("未核销");
            }
            row.add(codeDTO.getSenderName());
            rows.add(row);
        });
        data.setRows(rows);
        String now = DateUtils.dateToString(new Date(), DateUtils.DATE_HOUR_SECOND);
        ExcelUtil.exportExcel(response, "优惠券发放记录-" + now, data);
    }

    public List<CouponDTO> parkingCouponList(){
        CouponRequestBody couponRequestBody = new CouponRequestBody();
        String postJson = Request.postJson(storeUrl + "coupon/query_parking_coupon_list", couponRequestBody);
        JSONObject jsonObject = JSONObject.parseObject(postJson);
        Integer resCode = jsonObject.getInteger("resCode");
        List<CouponDTO> pageDTO = null;
        if (0 == resCode) {
            return jsonObject.getJSONArray("data").toJavaList(CouponDTO.class);
        }
        return pageDTO;
    }

    public List<CouponDTO> couponList(){
        CouponRequestBody couponRequestBody = new CouponRequestBody();
        String postJson = Request.postJson(storeUrl + "coupon/query_coupon_list", couponRequestBody);
        JSONObject jsonObject = JSONObject.parseObject(postJson);
        Integer resCode = jsonObject.getInteger("resCode");
        List<CouponDTO> pageDTO = null;
        if (0 == resCode) {
            return jsonObject.getJSONArray("data").toJavaList(CouponDTO.class);
        }
        return pageDTO;
    }

}
